package com.github.danielwegener.logback.kafka.enums;


import com.github.danielwegener.logback.kafka.codec.Encoding;

import java.util.List;

public enum BytesMessageEncoder {
    JSON {
        @Override
        public byte[] encode(List<byte[]> values) {
            int sizeOfArray = 2;
            int length = values.size();
            int ix = 0;

            while(ix < length) {
                sizeOfArray += ((byte[])values.get(ix++)).length;
                if (ix < length) {
                    ++sizeOfArray;
                }
            }

            byte[] buf = new byte[sizeOfArray];
            int posx = 0;
            int pos = posx + 1;
            buf[posx] = 91;
            int i = 0;

            while(i < length) {
                byte[] v = (byte[])values.get(i++);
                System.arraycopy(v, 0, buf, pos, v.length);
                pos += v.length;
                if (i < length) {
                    buf[pos++] = 44;
                }
            }

            buf[pos] = 93;
            return buf;
        }
    },
    /** @deprecated */
    @Deprecated
    THRIFT {
        @Override
        public byte[] encode(List<byte[]> values) {
            int sizeOfArray = 5;
            int length = values.size();

            for(int ix = 0; ix < length; ++ix) {
                sizeOfArray += ((byte[])values.get(ix)).length;
            }

            byte[] buf = new byte[sizeOfArray];
            int posx = 0;
            int pos = posx + 1;
            buf[posx] = 12;
            buf[pos++] = (byte)(length >>> 24 & 255);
            buf[pos++] = (byte)(length >>> 16 & 255);
            buf[pos++] = (byte)(length >>> 8 & 255);
            buf[pos++] = (byte)(length & 255);

            byte[] v;
            for(int i = 0; i < length; pos += v.length) {
                v = (byte[])values.get(i++);
                System.arraycopy(v, 0, buf, pos, v.length);
            }

            return buf;
        }
    },
    PROTO3 {
        @Override
        public byte[] encode(List<byte[]> values) {
            int sizeOfArray = 0;
            int length = values.size();

            for(int ix = 0; ix < length; sizeOfArray += ((byte[])values.get(ix++)).length) {
            }

            byte[] buf = new byte[sizeOfArray];
            int pos = 0;

            byte[] v;
            for(int i = 0; i < length; pos += v.length) {
                v = (byte[])values.get(i++);
                System.arraycopy(v, 0, buf, pos, v.length);
            }

            return buf;
        }
    };

    private BytesMessageEncoder() {
    }

    public abstract byte[] encode(List<byte[]> var1);

    public static BytesMessageEncoder forEncoding(Encoding encoding) {
        if (encoding == null) {
            throw new NullPointerException("encoding == null");
        } else {
            switch(encoding) {
            case JSON:
                return JSON;
            case PROTO3:
                return PROTO3;
            case THRIFT:
                return THRIFT;
            default:
                throw new UnsupportedOperationException(encoding.name());
            }
        }
    }
}
